package com.msk.web.controller.wlhy;

import com.google.common.collect.Maps;
import com.msk.base.domain.PriceCompare;
import com.msk.base.domain.goods.Goods;
import com.msk.base.service.PriceCompareService;
import com.msk.base.service.goods.GoodsService;
import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.common.utils.StringUtils;
import com.msk.framework.web.utils.TokenUtils;
import com.msk.sys.domain.Dict;
import com.msk.sys.utils.DictMskUtils;
import com.msk.wlhy.domain.HtDriverFile;
import com.msk.wlhy.domain.HtSupplier;
import com.msk.wlhy.service.HtDriverFileService;
import com.msk.wlhy.service.HtSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author llh
 * @date 2023/5/9
 * @description 货源询价接口
 */
@Api(tags = "货源询价接口")
@RestController
@RequestMapping("/api/supplierSourceInquiry")
public class SupplierSourceInquiryController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private HtSupplierService htSupplierService;

    @Autowired
    private HtDriverFileService htDriverFileService;

    @Autowired
    private PriceCompareService priceCompareService;


    /**
     * @author llh
     * @date 2023/5/10
     * @return com.msk.common.core.domain.R<?>
     * @description 货源询价列表接口
     */
    @ApiOperation("货源询价列表接口")
    @PostMapping("/goodsSourceList")
    public R<?> goodsSourceList(@RequestBody Map<String, Object> params) {
        HtSupplier supplierLogin = TokenUtils.getSupplierLogin();
        HtDriverFile driverLogin = TokenUtils.getDriverLogin();
        //获取登录用户
        LoginUser loginUser = TokenUtils.getMpLoginUser();
        String userId = loginUser.getLoginId();
        String supplierUser = null;
        String userIdentity = loginUser.getLoginObject();
        if ("1".equals(userIdentity)) {
            if (driverLogin == null) {
                return R.appFail(400101, "用户未认证");
            } else if ("0".equals(driverLogin.getIsPassCheck()) || "2".equals(driverLogin.getIsPassCheck())) {
                return R.appFail(400101, "用户未认证");
            }
            HtDriverFile htDriverFile = htDriverFileService.getDriverByUserId(userId);
            supplierUser = htDriverFile.getId();
        } else if ("3".equals(userIdentity)) {
            if (supplierLogin == null || !supplierLogin.getAuditStatus().equals("1")) {
                return R.appFail(400101, "用户未认证");
            }
            HtSupplier htSupplier = htSupplierService.getHtSupplierByUserId(userId);
            supplierUser = htSupplier.getId();
        }
        String biddingStatus = null;
        if (StringUtils.isNotNull(params.get("biddingStatus"))) {
            biddingStatus = StringUtils.getString(params.get("biddingStatus"));
        }
        params.put("orderByColumn", "create_date");
        params.put("isAsc", "desc");
        startPageJson(params, "bg");
        List<Map<String, Object>> list = goodsService.selectSourceInquiryListByContact(supplierUser, biddingStatus);
        return R.appOk(getDataTable(list));
    }

    /**
     * @author llh
     * @date 2023/5/11
     * @return com.msk.common.core.domain.R<?>
     * @description
     */
    @ApiOperation("司机货源&承运商货源竞价报价接口")
    @PostMapping("/priceCompareSave")
    public R<?> priceCompareSave(@RequestBody PriceCompare priceCompare) {
        if (StringUtils.isNull(priceCompare.getDriverPriceOffer())) {
            return R.appFail(400101, "参数driverPriceOffer#为空");
        }
        int i = 4;
        Map<String, Object> result = Maps.newHashMap();
        LoginUser user = TokenUtils.getMpLoginUser();
        try {
            String message = validateModel(priceCompare);
            if (StringUtils.isNotBlank(message)) {
                result.put("flag", false);
                result.put("message", message);
                return R.appFail(result, 2002);
            }
            priceCompareService.priceCompareSave(priceCompare, user);
            result.put("flag", true);
            result.put("message", "出价成功");
        } catch (Exception e) {
            result.put("flag", false);
            result.put("message", e.toString());
        }
        return R.appOk(result);
    }

    /**
     * @author llh
     * @date 2023/5/23
     * @return com.msk.common.core.domain.R<?>
     * @description 货源询价查看详情接口
     */
    @ApiOperation("货源询价查看详情接口")
    @PostMapping("/showGoodsDetails")
    public R<?> showGoodsDetails(@RequestBody Goods goods) {
        LoginUser loginUser = TokenUtils.getMpLoginUser();
        String loginId = null;
        if ("1".equals(loginUser.getLoginObject())) {
            loginId = TokenUtils.getDriverLogin().getId();
        }
        if ("3".equals(loginUser.getLoginObject())) {
            loginId = TokenUtils.getSupplierLogin().getId();
        }
        goods.setLoginId(loginId);
        Goods result = null;
        if (loginUser != null) {
            result = goodsService.selectGoodsById(goods);
            result.setPriceCompareId(goods.getPriceCompareId());
            String goodsType = result.getGoodsType();
            List<Dict> goodsTypeDictList = DictMskUtils.getDictListNew("goods_type");
            if (goodsType != null && StringUtils.isNotEmpty(goodsTypeDictList)) {
                String goodsTypeAlias = DictMskUtils.getDictLabelNew(goodsType, goodsTypeDictList, "");
                result.setGoodsTypeAlias(goodsTypeAlias);
            }
        }
        return R.appOk(result);
    }

    /**
     * @author llh
     * @date 2023/6/30
     * @return com.msk.common.core.domain.R<?>
     * @description 删除询价数据
     */
    @ApiOperation("删除询价数据")
    @PostMapping("/deleteSourcePrice")
    public R<?> deleteSourcePrice(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        if (StringUtils.isBlank(id)) {
            return R.appFail(400101, "请求参数询价ID#为空");
        }
        priceCompareService.delete(new PriceCompare(id));
        return R.appOk("删除询价成功");
    }
}
